﻿using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;

namespace BlazorCookieAuthenticationApp;
[ApiController]
[Route("api/auth")]
public class AuthController : ControllerBase
{
    [HttpPost]
    public async Task<IResult> Auth()
    {
        var identity = new ClaimsIdentity("Cookie", ClaimTypes.Name, ClaimTypes.Role);
        identity.AddClaim(new(ClaimTypes.Name, "admin"));

        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

        return Results.Redirect("/");
    }

    [HttpGet("out")]
    public async Task<IResult> SignOut()
    {
        await HttpContext.SignOutAsync();
        return Results.Redirect("/");
    }
}
